﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using System.Drawing;
using System.Globalization;
using MapCenterSystem.DataInfo;
using MapCenterSystem.DefineType;
using MapCenterSytem.Process;
using System.Collections.Generic;
using MapCenterSystem.Utility;

namespace MobiMap.Guest
{
    public partial class ResultService : System.Web.UI.Page
    {
        private string strService = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                HtmlControl divToolbarWrapper = (HtmlControl)Master.FindControl("divToolbarWrapper");
                divToolbarWrapper.Style["top"] = "6px";

                HtmlControl divToolbar = (HtmlControl)Master.FindControl("divToolbar");
                divToolbar.Style["display"] = "none";

                //Panel divEdge = (Panel)Master.FindControl("divEdge");
                //LiteralControl imgEdge = (LiteralControl)divEdge.Controls[0];
                //imgEdge.Text = "<div><img id=\"idEdge\" src=\"../Image/Expand.png\"  alt=\"edge\"/></div>";

                int pageSize = 5;
                if (Request.Cookies["mobimap_scrHeight"] != null)
                {
                    int scrHeight = int.Parse(Request.Cookies["mobimap_scrHeight"].Value);
                    if (scrHeight > 512)
                        scrHeight = 512;

                    pageSize = (scrHeight - 150) / 34;
                }
                gvService.PageSize = pageSize;

                if (Session["mobimap_service"] != null)
                {
                    string[] arr = (string[])Session["mobimap_service"];
                    strService = arr[0];
                    lbKeyWord.Text = strService;
                }
                else
                {
                    Response.Redirect("Home.aspx");
                    return;
                }

                Cache.Remove("mobimap_listService");
                BindListService(strService);
            }
            else
            {
                FillKeyword();
            }
        }
                
        public void BindListService(String keyword)
        {
            keyword = StringHelper.GetSearchString(keyword, AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["abbr_file"]);
            IList<PlaceInfo> lstPlaceInfo = new List<PlaceInfo>();
            if (Cache["mobimap_listService"] == null)
            {
                string[] s = (string [])Session["mobimap_nearbyinfo"];
                double longitude = double.Parse(s[2], CultureInfo.InvariantCulture);
                double latitude = double.Parse(s[3], CultureInfo.InvariantCulture);
                PlaceTypeInfo type = PlaceTypeEx.GetPlaceTypeByDescription(keyword);
                if (type != null)
                {
                    lstPlaceInfo = PlaceEx.GetPlaceByTypeAround(type.ID,
                                    new Coordinate(latitude, longitude), (float)0.5);/////
                }

                Cache["mobimap_listService"] = lstPlaceInfo;
            }
            else
            {
                lstPlaceInfo = (IList<PlaceInfo>)Cache["mobimap_listService"];
            }

            gvService.DataSource = lstPlaceInfo;
            gvService.DataBind();
        }


        public void FillKeyword()
        {
            if (Session["mobimap_service"] != null)
            {
                string[] arr = (string[])Session["mobimap_service"];
                strService = arr[0];
            }

            lbKeyWord.Text = strService;
        }
        
        public void lbtService_Click(Object sender, EventArgs e)
        {
            LinkButton link = (LinkButton)sender;
            Control selectedRow = (Control)link.Parent;

            Label lbFullName = (Label)selectedRow.FindControl("lbFullName");
            Label lbAddress = (Label)selectedRow.FindControl("lbAddress");
            Label lbLong = (Label)selectedRow.FindControl("lbLongitude");
            Label lbLat = (Label)selectedRow.FindControl("lbLatitude");
            Label lbID = (Label)selectedRow.FindControl("serID");
            
            string[] serviceInfo = { lbFullName.Text, lbAddress.Text, lbLong.Text, lbLat.Text, lbID.Text };
            Session["mobimap_homeinfo"] = serviceInfo;

            Response.Redirect("Home.aspx");
        }

        protected void gvService_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvService.PageIndex = e.NewPageIndex;
            BindListService(strService);
        }
        public void All_Click(Object sender, EventArgs e)
        {
            List<PlaceInfo> lstPlaceInfo = new List<PlaceInfo>();
            lstPlaceInfo = (List<PlaceInfo>)Cache["mobimap_listService"];

            Session["mobimap_lstService"] = lstPlaceInfo;

            Response.Redirect("Home.aspx");
        }

    }
}
